package com.css.fxfzaqbz.modules.assess.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.BigDisasterEntity;
import com.css.fxfzaqbz.modules.assess.repository.BigDisasterRepository;
import com.css.fxfzaqbz.modules.assess.service.BigDisasterService;
import com.css.fxfzaqbz.modules.assess.service.DisasterService;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.service.ImportantActivityService;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author y1fan
 * @create 2022-09-19 17:01
 */
@Service
public class BigDisasterServiceImpl implements BigDisasterService {

    @Resource
    private DisasterService disasterService;
    @Resource
    private BigDisasterRepository bigDisasterRepository;
    @Resource
    private ImportantActivityService importantActivityService;

    @Override
    public BigDisasterEntity getBigDisasterByActiveId(String id) {
        //判断数据库里是否已经存在这一个大表
        BigDisasterEntity bigDisasterByActiveId = bigDisasterRepository.getBigDisasterByActiveId(id);

        if (bigDisasterByActiveId != null) {
            //已经存在
            bigDisasterByActiveId.setBdActiveId(id);
            bigDisasterByActiveId.setBdId(UUIDGenerator.getUUID());
            bigDisasterByActiveId.setBdOpinionStatus(importantActivityService.getImportantActivityById(id).getIaOpinionStatus());
            bigDisasterByActiveId.setBdOpinion(importantActivityService.getImportantActivityById(id).getIaOpinion());
            bigDisasterByActiveId.setEntities(disasterService.getAllDisasterAss(id));
            return bigDisasterByActiveId;
        } else {
            //不存在
            BigDisasterEntity bigDisasterEntity1 = new BigDisasterEntity();
            bigDisasterEntity1.setEntities(disasterService.getAllDisasterAss(id));
            bigDisasterEntity1.setBdOpinionStatus(importantActivityService.getImportantActivityById(id).getIaOpinionStatus());
            bigDisasterEntity1.setBdOpinion(importantActivityService.getImportantActivityById(id).getIaOpinion());
            return bigDisasterEntity1;
        }
    }

    @Override
    public void saveTask(ImportantActivity importantActivity1) {
        BigDisasterEntity disasterEntity = bigDisasterRepository.getBigDisasterByActiveId(importantActivity1.getIaId());
        if (disasterEntity == null) {
            BigDisasterEntity bigDisasterEntity = new BigDisasterEntity();
            bigDisasterEntity.setBdId(UUIDGenerator.getUUID());
            bigDisasterEntity.setContent("房屋地震灾害风险评估");
            bigDisasterEntity.setBdStatus(YNEnum.N.toString());
            bigDisasterEntity.setBdActiveId(importantActivity1.getIaId());
            bigDisasterEntity.setBdDelFlag(YNEnum.N.toString());
            bigDisasterEntity.setBdCreateTime(PlatformDateUtils.getCurrentTimestamp());
            bigDisasterEntity.setBdCreateUser(PlatformSessionUtils.getUserId());
            bigDisasterEntity.setBdOpinion(importantActivity1.getIaOpinion());
            bigDisasterRepository.saveBigDisaster(bigDisasterEntity);
        }

    }

    @Override
    public void upStatus(String id) {
        bigDisasterRepository.upStatus(id);
    }
}
